package com.hh.procure.controller;

import com.hh.procure.Util.FileConfig;
import com.hh.procure.Util.FileUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.OSUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
public class FileController {
    /**
     * 资源映射路径 前缀
     */
    @Value("${myFile.prefix}")
    public String localFilePrefix;

    /**
     * 域名或本机访问地址
     */
    @Value("${myFile.domain}")
    public String domain;

    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${myFile.linuxPath}")
    private String linuxPath;
    @Value("${myFile.winPath}")
    private String winPath;

    @Resource
    private FileConfig fileConfig;

    @GetMapping("/downloadFiles")
    public void downloadFiles(@RequestParam("file") String file, HttpServletResponse response) {
        if(OSUtils.isWindows()){
            FileUtil.downloadFiles(winPath, file, response);
        }else{
            FileUtil.downloadFiles(linuxPath, file, response);
        }
    }

    @RequestMapping("/RemoteFileDownloader")
    public void RemoteFileDownloader(@RequestParam("file") String file, HttpServletResponse response) {
        FileUtil.RemoteFileDownloader(file, response);
    }

    @PostMapping("/upload")
    public R handleFileUpload(MultipartFile file) {
        return fileConfig.ruoyiFileUpload(file);
    }


}
